//
//  DetailDayView.swift
//  StoreProject
//
//  Created by CYC on 2021/9/29.
//

import UIKit

class DetailDayView: UIButton {

    var model = DayModel()
    var todayView = UIView()    // 今日的红色背景
    var noticeView = UIView()   // 事件点
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        NotificationCenter.default.addObserver(self, selector: #selector(notifiAction), name: NSNotification.Name(rawValue: kNoticeChange), object: nil)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("")
    }
    
    // MARK:监听提醒事件
    @objc func notifiAction() {
        
        // 查看是否有提醒事件
        var noticeModel: DateNoticeModel?
        for item in DateNoticeModel.getDatas() {
            
            if item.year.integerValue == model.date.year &&
                item.month.integerValue == model.date.month &&
                item.day.integerValue == model.date.day
                {
                noticeModel = item
                break
            }
        }
        
        if noticeModel != nil {
            
            self.noticeView.isHidden = false
            
        } else {
            
            self.noticeView.isHidden = true
            
        }
        
        
        
    }
    
    // MARK:创建UI
    func creatUIAction(model: DayModel) {
        
        self.model = model
        self.removeAllSubviews()
        
        var color1: UIColor?   // 新历、农历
        var color2: UIColor?   // 节日
        var color3: UIColor?   // 提醒
        var flag1 = false      // 是否显示今天
        var flag2 = false      // 是否显示大年初一
        var flag3 = false      // 是否显示初一
        var flag4 = false      // 是否显示提醒
        
        // 今天
        if model.date.year == kTodayCom.year && model.date.month == kTodayCom.month && model.date.day == kTodayCom.day {
            
            color1 = .white
            color2 = .white
            color3 = .white
            flag1 = true        // 是今天就不显示初一
            flag2 = false
            flag3 = false
            
        } else {
            
            flag1 = false
            color2 = kMainColor
            color3 = kMainColor
            
            // 周日、周六的颜色是灰色
            if model.date.weekday == 1 || model.date.weekday == 7 {
                
                color1 = kLabelColor2
                
            } else {
                
                color1 = kLabelColor1
            }
            
            // 农历初一
            if model.chineseSubDay == "初一" {
                
                if model.chineseSubMonth == "正月" {

                    flag2 = true
                    flag3 = false
                } else {

                    flag2 = false
                    flag3 = true
                }
                
            }
        }
        
        
        
        // 查看是否有提醒事件
        var noticeModel: DateNoticeModel?
        for item in DateNoticeModel.getDatas() {
            
            if item.year.integerValue == model.date.year &&
                item.month.integerValue == model.date.month &&
                item.day.integerValue == model.date.day
                {
                noticeModel = item
                break
            }
        }
        
        if noticeModel != nil {
            
            flag4 = true
            
        } else {
            
            flag4 = false
            
        }
    
        
        
        
        
        
        // 顶部分割线
        let line1 = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.c_width(), height: kLineSize))
        line1.backgroundColor = kLineColor
        self.addSubview(line1)
        
        // 新历
        let lab1 = UILabel.init(frame: CGRect.init(x: 0, y: 10, width: self.c_width(), height: 20))
        lab1.textAlignment = .center
        lab1.textColor = color1
        lab1.font = kBFont(15)
        lab1.text = String.init(format: "%d", model.day)
        self.addSubview(lab1)
        
        // 农历
        let lab2 = UILabel.init(frame: CGRect.init(x: 0, y: lab1.c_bottom(), width: self.c_width(), height: 15))
        lab2.textColor = color1
        lab2.textAlignment = .center
        lab2.font = kRFont(10)
        lab2.text = model.chineseSubDay == "初一" ? model.chineseSubMonth : model.chineseSubDay
        self.addSubview(lab2)
        
        // 节日
        let lab3 = UILabel.init(frame: CGRect.init(x: 0, y: lab2.c_bottom(), width: self.c_width(), height: 12))
        lab3.textColor = color2
        lab3.textAlignment = .center
        lab3.font = kRFont(8)
        lab3.text = model.specialDay
        self.addSubview(lab3)
        
        // 提醒
        let size = CGFloat(6)
        let noticeView = UIView.init(frame: .init(x: (self.c_width() - size)/2.0, y: lab2.c_bottom() + 2, width: size, height: size))
        noticeView.isUserInteractionEnabled = false
        noticeView.backgroundColor = color3
        noticeView.isHidden = !flag4
        noticeView.c_radius(radius: size/2.0)
        self.addSubview(noticeView)
        self.noticeView = noticeView
        
        // 今天
        var pointViewSize = self.c_width()
        if pointViewSize > self.c_height() {
            pointViewSize = self.c_height()
        }
        let pointView = UIView.init(frame: .init(x: (self.c_width() - pointViewSize)/2.0, y: (self.c_height() - pointViewSize)/2.0, width: pointViewSize, height: pointViewSize))
        pointView.c_radius(radius: pointViewSize/2.0)
        pointView.isUserInteractionEnabled = false
        pointView.backgroundColor = kMainColor
        pointView.isHidden = !flag1
        self.insertSubview(pointView, at: 0)
        self.todayView = pointView
        
        let size2 = lab2.text!.boundingRect(with: .init(width: kScreenWidth, height: CGFloat(MAXFLOAT)),
                                     options: .usesLineFragmentOrigin,
                                     attributes: [NSAttributedString.Key.font : kMFont(10)],
                                     context: nil);
        
        // 大年初一
        let line2 = UIView.init(frame: CGRect.init(x: (self.c_width() - size2.width)/2.0, y: lab2.c_bottom(), width: size2.width, height: kLineSize*2))
        line2.backgroundColor = kMainColor
        line2.isUserInteractionEnabled = false
        line2.isHidden = !flag2
        self.addSubview(line2)
        
        // 初一
        let line3 = UIView.init(frame: CGRect.init(x: (self.c_width() - size2.width)/2.0, y: lab2.c_bottom(), width: size2.width, height: kLineSize))
        line3.backgroundColor = kMainColor
        line3.isUserInteractionEnabled = false
        line3.isHidden = !flag3
        self.addSubview(line3)
        
        
        
        
        
    }

}
